package com.jr.daoimpl;

import com.jr.dao.UserDao;
import com.jr.entity.Users;
import com.jr.pojo.UserEnterprise;
import util.DBHelper;

import java.util.ArrayList;

public class UserDaoImpl implements UserDao {
    DBHelper db = new DBHelper();

    @Override
    public Users selectUser(String account, String password) {
        String sql = "select * from users where account=? and password=?";
        Object[] objects = {account, password};
        ArrayList<Users> list = db.query(sql, Users.class, objects);
        if (list.size() > 0) {
            return (Users) list.get(0);
        } else {
            return null;
        }
    }

    @Override
    public ArrayList<Users> login(String account, String password) {
        String sql = "select * from user where account=? and password=?";
        Object[] objects = {account, password};
        return db.query(sql, Users.class, objects);
    }


    @Override
    public UserEnterprise select(String account, String password) {
        String sql = "select u.*,e.*\n" +
                "from user u inner join enterprise e\n" +
                "on u.enterprise_name = e.enamess\n" +
                "where u.account=? and u.password=?";
        Object[] objects = {account, password};
        ArrayList<UserEnterprise> list = db.query(sql, UserEnterprise.class, objects);
        /*System.out.println(list);*/
        if (list.size() > 0) {
            return (UserEnterprise) list.get(0);
        } else {
            return null;
        }
    }


    @Override
    public int adduser(int enterprise_id, String ename, String account, String password, String idcard_name, String idcard_no) {
        String sql = "update users set enterprise_id=?,enterprise_name=?,idcard_name=?,idcard_no=? where account=? and password=?";
        Object[] objects = {enterprise_id, ename, idcard_name, idcard_no, account, password};
        return db.update(sql, objects);
    }


}
